//
//  LoginViewController.m
//  Faner
//
//  Created by apple on 15/10/29.
//  Copyright © 2015年 lanou3g.com蓝鸥科技有限公司. All rights reserved.
//

#import "LoginViewController.h"
#import "PKRequestManager.h"
#import "ResgitViewController.h"
#define mainSize    [UIScreen mainScreen].bounds.size

#define offsetLeftHand      60

#define rectLeftHand        CGRectMake(61-offsetLeftHand, 90, 40, 65)
#define rectLeftHandGone    CGRectMake(mainSize.width / 2 - 100, vLogin.frame.origin.y - 22, 40, 40)

#define rectRightHand       CGRectMake(imgLogin.frame.size.width / 2 + 60, 90, 40, 65)
#define rectRightHandGone   CGRectMake(mainSize.width / 2 + 62, vLogin.frame.origin.y - 22, 40, 40)
@interface LoginViewController ()<UITextFieldDelegate>
{
    UITextField* txtUser;
    UITextField* txtPwd;
    
    UIImageView* imgLeftHand;
    UIImageView* imgRightHand;
    
    UIImageView* imgLeftHandGone;
    UIImageView* imgRightHandGone;
    
    JxbLoginShowType showType;
}
@property (nonatomic, strong) UIButton *loginBtn;
@property (nonatomic, strong) UIButton *signupBtn;
@end

@implementation LoginViewController

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [txtPwd resignFirstResponder];
    [txtUser resignFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [txtPwd resignFirstResponder];
    [txtUser resignFirstResponder];
    return YES;


}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:0.212 green:0.742 blue:1.000 alpha:1.000];
    
    UIImageView* imgLogin = [[UIImageView alloc] initWithFrame:CGRectMake(mainSize.width / 2 - 211 / 2, 100, 211, 109)];
    imgLogin.image = [UIImage imageNamed:@"owl-login"];
    imgLogin.layer.masksToBounds = YES;
    [self.view addSubview:imgLogin];
    
    imgLeftHand = [[UIImageView alloc] initWithFrame:rectLeftHand];
    imgLeftHand.image = [UIImage imageNamed:@"owl-login-arm-left"];
    [imgLogin addSubview:imgLeftHand];
    
    imgRightHand = [[UIImageView alloc] initWithFrame:rectRightHand];
    imgRightHand.image = [UIImage imageNamed:@"owl-login-arm-right"];
    [imgLogin addSubview:imgRightHand];
    
    UIView* vLogin = [[UIView alloc] initWithFrame:CGRectMake(15, 200, mainSize.width - 30, 160)];
    vLogin.layer.borderWidth = 0.5;
    vLogin.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    vLogin.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:vLogin];
    
    imgLeftHandGone = [[UIImageView alloc] initWithFrame:rectLeftHandGone];
    imgLeftHandGone.image = [UIImage imageNamed:@"icon_hand"];
    [self.view addSubview:imgLeftHandGone];
    
    imgRightHandGone = [[UIImageView alloc] initWithFrame:rectRightHandGone];
    imgRightHandGone.image = [UIImage imageNamed:@"icon_hand"];
    [self.view addSubview:imgRightHandGone];
    
    txtUser = [[UITextField alloc] initWithFrame:CGRectMake(30, 30, vLogin.frame.size.width - 60, 44)];
    txtUser.delegate = self;
    txtUser.layer.cornerRadius = 5;
    txtUser.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    txtUser.placeholder = @"请输入你的邮箱";
    txtUser.layer.borderWidth = 0.5;
    txtUser.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    txtUser.leftViewMode = UITextFieldViewModeAlways;
    UIImageView* imgUser = [[UIImageView alloc] initWithFrame:CGRectMake(11, 11, 22, 22)];
    imgUser.image = [UIImage imageNamed:@"iconfont-user"];
    [txtUser.leftView addSubview:imgUser];
    [vLogin addSubview:txtUser];
    
    txtPwd = [[UITextField alloc] initWithFrame:CGRectMake(30, 90, vLogin.frame.size.width - 60, 44)];
    txtPwd.delegate = self;
    txtPwd.placeholder = @"请输入你的密码";
    txtPwd.layer.cornerRadius = 5;
    txtPwd.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    txtPwd.layer.borderWidth = 0.5;
    txtPwd.secureTextEntry = YES;
    txtPwd.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    txtPwd.leftViewMode = UITextFieldViewModeAlways;
    UIImageView* imgPwd = [[UIImageView alloc] initWithFrame:CGRectMake(11, 11, 22, 22)];
    imgPwd.image = [UIImage imageNamed:@"iconfont-password"];
    [txtPwd.leftView addSubview:imgPwd];
    [vLogin addSubview:txtPwd];
    
    
    self.returnBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.returnBtn.frame = CGRectMake(10, 20, 40, 30);
    self.returnBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.returnBtn setTitle:@"返回" forState:UIControlStateNormal];
    [self.returnBtn setTitleColor:[UIColor colorWithRed:0.948 green:0.456 blue:0.748 alpha:1.000] forState:UIControlStateNormal];
    [self.returnBtn addTarget:self action:@selector(returnMainview) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.returnBtn];
    
    self.loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.loginBtn.frame = CGRectMake(47*SCREEN_WIDTH/375, 456*SCREEN_HEIGHT/667, 282*SCREEN_WIDTH/375, 51*SCREEN_HEIGHT/667);
    self.loginBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.loginBtn setBackgroundColor:[UIColor colorWithRed:0.697 green:0.659 blue:0.670 alpha:1.000]];
    [self.loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [self.loginBtn setTitleColor:[UIColor colorWithRed:0.174 green:0.169 blue:0.168 alpha:1.000] forState:UIControlStateNormal];
    [self.loginBtn addTarget:self action:@selector(checkPassword) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.loginBtn];
    
    self.signupBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.signupBtn.frame = CGRectMake(47*SCREEN_WIDTH/375, 535*SCREEN_HEIGHT/667, 282*SCREEN_WIDTH/375, 51*SCREEN_HEIGHT/667);
    self.signupBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.signupBtn setTitle:@"注册" forState:UIControlStateNormal];
    [self.signupBtn setBackgroundColor:[UIColor colorWithRed:0.697 green:0.659 blue:0.670 alpha:1.000]];
    [self.signupBtn setTitleColor:[UIColor colorWithRed:0.174 green:0.169 blue:0.168 alpha:1.000] forState:UIControlStateNormal];
    [self.signupBtn addTarget:self action:@selector(toSignUpView) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.signupBtn];
    
    
}
- (void)returnMainview
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)checkPassword
{// 核实密码
    NSString *userStr = txtUser.text;
    NSString *passwordStr = txtPwd.text;
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"email"] = userStr;
    parDic[@"passwd"] = passwordStr;
    [PKRequestManager PKRequestManagerrequsetWithType:POST URLString:@"http://api2.pianke.me/user/login" ParDIc:parDic finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        if ([dic[@"result"] integerValue] == 0) {
            UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:dic[@"data"][@"msg"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alter show];
        }else{
            NSString *auth = dic[@"data"][@"auth"];
            NSString *uname = dic[@"data"][@"uname"];
            [[NSUserDefaults standardUserDefaults]setValue:auth forKey:@"auth"];
            NSLog(@"====%@",[[NSUserDefaults standardUserDefaults]stringForKey:@"auth"]);
            [[NSUserDefaults standardUserDefaults]setValue:uname forKey:@"uname"];
            [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"state"];
            NSLog(@"%d",[[NSUserDefaults standardUserDefaults]boolForKey:@"state"]);
            [[NSNotificationCenter defaultCenter]postNotificationName:@"Login" object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:uname,@"name", nil]];
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    } error:^(NSError *error) {
        
    }];
}
- (void)toSignUpView
{  // 跳转注册界面
    NSLog(@"2222");
    ResgitViewController *resgisterVC = [[ResgitViewController alloc]init];
    [self presentViewController:resgisterVC animated:YES completion:nil];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if ([textField isEqual:txtUser]) {
        if (showType != JxbLoginShowType_PASS)
        {
            showType = JxbLoginShowType_USER;
            return;
        }
        showType = JxbLoginShowType_USER;
        [UIView animateWithDuration:0.5 animations:^{
            imgLeftHand.frame = CGRectMake(imgLeftHand.frame.origin.x - offsetLeftHand, imgLeftHand.frame.origin.y + 30, imgLeftHand.frame.size.width, imgLeftHand.frame.size.height);
            
            imgRightHand.frame = CGRectMake(imgRightHand.frame.origin.x + 48, imgRightHand.frame.origin.y + 30, imgRightHand.frame.size.width, imgRightHand.frame.size.height);
            
            
            imgLeftHandGone.frame = CGRectMake(imgLeftHandGone.frame.origin.x - 70, imgLeftHandGone.frame.origin.y, 40, 40);
            
            imgRightHandGone.frame = CGRectMake(imgRightHandGone.frame.origin.x + 30, imgRightHandGone.frame.origin.y, 40, 40);
            
            
        } completion:^(BOOL b) {
        }];
        
    }
    else if ([textField isEqual:txtPwd]) {
        if (showType == JxbLoginShowType_PASS)
        {
            showType = JxbLoginShowType_PASS;
            return;
        }
        showType = JxbLoginShowType_PASS;
        [UIView animateWithDuration:0.5 animations:^{
            imgLeftHand.frame = CGRectMake(imgLeftHand.frame.origin.x + offsetLeftHand, imgLeftHand.frame.origin.y - 30, imgLeftHand.frame.size.width, imgLeftHand.frame.size.height);
            imgRightHand.frame = CGRectMake(imgRightHand.frame.origin.x - 48, imgRightHand.frame.origin.y - 30, imgRightHand.frame.size.width, imgRightHand.frame.size.height);
            
            
            imgLeftHandGone.frame = CGRectMake(imgLeftHandGone.frame.origin.x + 70, imgLeftHandGone.frame.origin.y, 0, 0);
            
            imgRightHandGone.frame = CGRectMake(imgRightHandGone.frame.origin.x - 30, imgRightHandGone.frame.origin.y, 0, 0);
            
        } completion:^(BOOL b) {
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
